package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.dto.CompanyDto;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.ICompanyService;
import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 服务逻辑层
 */
public class CompanyServiceImpl implements ICompanyService {

    @Override
    public ResultModel add(CompanyDto companyDto) {
        CompanyModel companyModel = new CompanyModel();
        companyModel.setLogo(companyDto.getLogo());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss sss");
        System.out.println(new Date().toString());
        companyModel.setCreateTime(simpleDateFormat.format(new Date()));
        companyModel.setName(companyDto.getName());
        companyModel.setAd(companyDto.getAd());
        companyModel.setPhone(companyDto.getPhone());
        companyModel.setCompanyIntroduce(companyDto.getContent());
//        调用数据接口层
        CompanyDao companyDao = new CompanyImpl();

        ResultModel resultModel = new ResultModel();
        resultModel.setObject(companyDao.add(companyModel));

        return resultModel;
    }

    static final Object object = new Object();

    /**
     * 首先去redis里面查询，是否已经在缓存里面
     * 如果有，则直接取缓存
     * 如果没有则查询数据库，并存入缓存
     *
     * @return
     */
    @Override
    public CompanyModel getCompanyModel() {
        CompanyDao companyDao = new CompanyImpl();
        Jedis jedis = new Jedis();
        final CompanyModel[] companyModel = {new CompanyModel()};
        int expriseTime = 10;
        boolean isHave = jedis.exists("id");
        if (isHave) {
            companyModel[0].setLogo(jedis.get("logo"));
            companyModel[0].setPhone(jedis.get("phone"));
            companyModel[0].setAd(jedis.get("ad"));
            companyModel[0].setNet(jedis.get("net"));
            companyModel[0].setEmail(jedis.get("email"));
            companyModel[0].setId(Integer.parseInt(jedis.get("id")));
            System.out.println("redis");
        } else {
            System.out.println("mysql");
            companyModel[0] = companyDao.getCompanyModel();
            jedis.set("id",companyModel[0].getId().toString());
            jedis.set("logo", companyModel[0].getLogo());
            jedis.set("phone", companyModel[0].getPhone());
            jedis.set("ad", companyModel[0].getAd());
            jedis.set("net", companyModel[0].getNet());
            jedis.set("email", companyModel[0].getEmail());

        }
//        for (int i = 0; i < 1000; i++) {
//
//            Thread thread = new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    synchronized (object) {
//                        boolean isHave = jedis.exists("id");
//                        if (isHave) {
//                            companyModel[0].setLogo(jedis.get("logo"));
//                            companyModel[0].setPhone(jedis.get("phone"));
//                            companyModel[0].setAd(jedis.get("ad"));
//                            companyModel[0].setNet(jedis.get("net"));
//                            companyModel[0].setEmail(jedis.get("email"));
//                            companyModel[0].setId(Integer.parseInt(jedis.get("id")));
//                            System.out.println("redis");
//                        } else {
//                            System.out.println("mysql");
//                            companyModel[0] = companyDao.getCompanyModel();
//                            jedis.set("id",companyModel[0].getId().toString());
//                            jedis.set("logo", companyModel[0].getLogo());
//                            jedis.set("phone", companyModel[0].getPhone());
//                            jedis.set("ad", companyModel[0].getAd());
//                            jedis.set("net", companyModel[0].getNet());
//                            jedis.set("email", companyModel[0].getEmail());
//
//                        }
//                    }
//                }
//            });
//            thread.start();
//        }
        return companyModel[0];
    }

    @Override
    public ResultModel findAll() {
        CompanyDao companyDao = new CompanyImpl();
        ResultModel resultModel = new ResultModel();
        resultModel.setObject(companyDao.findAll());
        return resultModel;
    }

    @Override
    public ResultModel findById(int id) {
        ResultModel resultModel = new ResultModel();
        CompanyDao companyDao = new CompanyImpl();
        resultModel.setObject(companyDao.findById(id));
        return resultModel;
    }

    @Override
    public ResultModel update(CompanyModel companyModel) {
        ResultModel resultModel = new ResultModel();
        CompanyDao companyDao = new CompanyImpl();
        resultModel.setObject(companyDao.update(companyModel));
        return resultModel;
    }

    @Override
    public ResultModel add(CompanyModel companyModel) {
        ResultModel resultModel = new ResultModel();
        CompanyDao companyDao = new CompanyImpl();
        resultModel.setObject(companyDao.add(companyModel));
        return resultModel;
    }
}
